﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommLib.Web.Page
{
    public static class Pagination
    {
        ///<summary>
        ///打印分页信息。
        ///</summary>
        ///<param name="recordCount">记录总数。</param>
        ///<param name="pageIndex">分页索引。</param>
        ///<param name="pageSize">分页大小。</param>
        ///<param name="pageLink">分页地址，{0}为当前页匹配。</param>
        ///<param name="nextPageText">下一页显示文本。</param>
        ///<param name="prevPageText">上一页显示文本。</param>
        ///<param name="homePageText">首页显示文本。</param>
        ///<param name="lastPageText">尾页显示文本。</param>
        ///<param name="displayMsg">显示消息文本。</param>
        ///<example><code>Pagination.Print(recordcount, 1, pagesize, "?page=", "下一页", "上一页", "首页", "末页", "")</code></example>
        ///<returns>返回分页的 Html 代码。</returns>
        public static string Print(int recordCount, int pageIndex, int pageSize, string pageLink, string nextPageText, string prevPageText, string homePageText, string lastPageText, string displayMsg="")
        {
            if (pageSize < 1)
                pageSize = 1; 
            if (pageIndex < 1)
                pageIndex = 1; 
            //开始
            var htmlLeftPage = string.Empty;
            var htmlRightPage = string.Empty;
            if (string.IsNullOrEmpty(pageLink))
                pageLink = "?"; 
            //开始计算
            var pageCount = (recordCount % pageSize == 0) ? recordCount / pageSize : (recordCount / pageSize) + 1;
            
            for (var i = 2; i >= 1; i--)
            {
                if (pageIndex - i >= 1)
                {
                    htmlLeftPage += $"<a href=\"{pageLink + (pageIndex - i)}\"><span>{pageIndex - i}</span></a>";
                }
            }
            for (var j = 1; j <= 4; j++)
            {
                if (pageIndex + j <= pageCount)
                {
                    htmlRightPage += $"<a href=\"{pageLink + (pageIndex + j)}\"><span>{pageIndex + j}</span></a>";
                }
            }
            var prevPage = pageIndex - 1;

            if (prevPage < 1)
                prevPage = 1; 

            var nextPage = pageIndex + 1;

            if (nextPage > pageCount)
                nextPage = pageCount; 

            //最后处理
            if (nextPage < 1)
                nextPage = 1; 

            if (pageCount < 1)
                pageCount = 1; 

            //计算结束
            var leftHtml =
                $"<a {(pageIndex != 1 ? $"href=\"{pageLink + 1}\"" : "")}><span>{homePageText}</span></a><a {(pageIndex != prevPage ? $"href=\"{pageLink + prevPage}\"" : "")}><span>{prevPageText}</span></a>{htmlLeftPage}";
            var rightHtml =
                $"{htmlRightPage}<a {(pageIndex != nextPage ? $"href=\"{pageLink + nextPage}\"" : "")}><span>{nextPageText}</span></a><a {(pageIndex != pageCount ? $"href=\"{pageLink + pageCount}\"" : "")}><span>{lastPageText}</span></a>";
            var html =
                $"<div class=\"cutpage\" id=\"page\">{leftHtml}<a  class=\"current\" id=\"cutpage\"><span>{pageIndex}</span></a>{rightHtml}</div><span>{string.Format(displayMsg, pageIndex, pageCount, recordCount)}</span>";

            return html;
        }
    }
}
